package com.scalar.forecast.tags;

import com.scalar.forecast.commons.SessionKeys;
import com.scalar.forecast.commons.Constants;
import com.scalar.chiptrack.utils.ConfigManager;

import javax.servlet.jsp.tagext.TagSupport;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.JspException;
import javax.servlet.http.HttpSession;


/** Tag prints the page number links
 *
 * Created by Veeru. 
 * Date: Feb 17, 2005
 * Time: 7:13:38 PM 
 */


public class PageNumbersTag extends TagSupport{


    String url = "";

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

   public int doEndTag() throws JspException {

        HttpSession session = pageContext.getSession();

        int pageNumber = 1;
        int totalRecords = 0;
        int recordsPerPage = 0;
        int currentPage = 1;
        int totalPages = 0;
        String chipTracContext = "/" + ConfigManager.getChipTracContext();

        JspWriter out = pageContext.getOut();

        if ( session.getAttribute( SessionKeys.REQUEST_PAGE_NUMBER ) != null ) {
            try {
                currentPage = Integer.parseInt( ( String ) session.getAttribute( SessionKeys.REQUEST_PAGE_NUMBER ) );
            } catch ( Exception e ) {
                currentPage = 0;
            }
        } else{
            currentPage =1;
        }


        if ( session.getAttribute( SessionKeys.REQUEST_TOTAL_RECS ) != null ) {
            try {
                totalRecords = Integer.parseInt( ( String ) session.getAttribute( SessionKeys.REQUEST_TOTAL_RECS ) );
            } catch ( Exception e ) {
                totalRecords = 0;
            }
        }
        recordsPerPage = Constants.SERACH_RESULTS_PER_SCREEN;
        String mCurrentPage = SessionKeys.REQUEST_PAGE_NUMBER;

        url = chipTracContext + url +"&"+mCurrentPage+"=";

        if ( totalRecords > 0 ) {

            if ( totalRecords%recordsPerPage == 0 ) {
                totalPages = totalRecords/recordsPerPage;
            } else {
                totalPages = totalRecords/recordsPerPage+1;
            }
            if ( totalPages > 1 ) {
                try {
                    out.println( "<a href='" + url + pageNumber + "' class='header' onmouseover=\"window.status='Click here to go to First Page';return true\" onmouseout=\"window.status='';return true\" title='Click here to go to First Page'><<</a>&nbsp;" );
                    while( pageNumber <= totalPages ) {
                        if ( currentPage == pageNumber ) {
                            out.println( pageNumber + "&nbsp");
                        }else {
                            out.println( "<a href='" + url + pageNumber + "' class='header' onmouseover=\"window.status='Click here to go to page " + pageNumber + " ';return true\" onmouseout=\"window.status='';return true\" title='Click here to go to page "+ pageNumber + "'>" + pageNumber + "</a>&nbsp;" );
                        }
                        pageNumber++;
                    }
                    out.println( "<a href='" + url + totalPages + "' class='header' onmouseover=\"window.status='Click here to go to Last Page';return true\" onmouseout=\"window.status='';return true\" title='Click here to go to Last Page'> >></a>&nbsp;" );
                } catch ( Exception e ){
                    e.printStackTrace();
                }
            }
        }
        return( EVAL_PAGE );
    }

}
